package com.yuzhi.master.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.yuzhi.common.annotation.Log;
import com.yuzhi.common.contant.CacheNames;
import com.yuzhi.common.core.controller.BaseController;
import com.yuzhi.common.core.domain.ApiResult;
import com.yuzhi.common.core.domain.entity.SysDictData;
import com.yuzhi.common.core.domain.entity.SysDictType;
import com.yuzhi.common.core.page.PageDataInfo;
import com.yuzhi.common.enums.BusinessTypeEnum;
import com.yuzhi.common.util.poi.ExcelUtil;
import com.yuzhi.common.util.redis.CacheUtils;
import com.yuzhi.system.domain.request.SysDictTypeReq;
import com.yuzhi.system.service.ISysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 数据字典信息
 *
 * @author auto
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    private final ISysDictTypeService dictTypeService;

    /**
     * 查询字典类型列表
     */
    @SaCheckPermission("system:dict:list")
    @PostMapping("/list")
    public ApiResult<PageDataInfo> list(@RequestBody SysDictTypeReq dictType) {
        return ApiResult.ok(dictTypeService.selectPageDictTypeList(dictType));
    }

    /**
     * 导出字典类型列表
     */
    @Log(title = "字典类型", businessType = BusinessTypeEnum.EXPORT)
    @SaCheckPermission("system:dict:export")
    @PostMapping("/export")
    public void export(@RequestBody SysDictTypeReq dictType, HttpServletResponse response) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil.exportExcel(list, "字典类型", SysDictType.class, response);
    }

    /**
     * 查询字典类型详细
     *
     * @param dictId 字典ID
     */
    @SaCheckPermission("system:dict:query")
    @GetMapping(value = "/{dictId}")
    public ApiResult<SysDictType> getInfo(@PathVariable String dictId) {
        return ApiResult.ok(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public ApiResult<Void> add(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return ApiResult.fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.insertDictType(dict);
        return ApiResult.ok();
    }

    /**
     * 修改字典类型
     */
    @SaCheckPermission("system:dict:edit")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public ApiResult<Void> edit(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return ApiResult.fail("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dictTypeService.updateDictType(dict);
        return ApiResult.ok();
    }

    /**
     * 删除字典类型
     *
     * @param dictIds 字典ID串
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{dictIds}")
    public ApiResult<Void> remove(@PathVariable String[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return ApiResult.ok();
    }

    /**
     * 刷新字典缓存
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.CLEAN)
    @DeleteMapping("/refreshCache")
    public ApiResult<Void> refreshCache() {
        dictTypeService.resetDictCache();
        return ApiResult.ok();
    }

    /**
     * 刷新字典缓存
     */
    //@SaCheckPermission("system:dict:query")
    @GetMapping("/getAllDict")
    public ApiResult<Object> getAllDict() {
        Map<String,List<SysDictData>> map = new HashMap<>();
        Object o1 = CacheUtils.get(CacheNames.SYS_DICT);
        return ApiResult.ok(o1);
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public ApiResult<List<SysDictType>> optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return ApiResult.ok(dictTypes);
    }
}
